/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.socio.behaviors;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.appengine.api.datastore.Key;
import com.socio.model.User;
import com.socio.utils.FieldAccessor;
import com.socio.utils.FieldAccessor.FieldAccessorField;

/**
 *
 * @author potlow
 */
public abstract class UserBehavior extends DAOBehavior {

    @Override
    public boolean create(Object value) {
        User user;
        if (value instanceof User) {
            user = (User) value;
        } else {
            throw new IllegalArgumentException("Search value must be a User");
        }

        // remove tour from cache if changes where persisted
        boolean persisted = persist(user);
        if(persisted) {
        	cache.remove(user.getKey());
        	if(!user.getAndroidUDID().isEmpty())
        		cache.remove(user.getAndroidUDID());
        	if(!user.getFacebookID().isEmpty())
        		cache.remove(user.getFacebookID());
        }
        
        return persisted;
    }

    @SuppressWarnings("unchecked")
	@Override
	public List<Key> getAllKeys() {
    	PersistenceManager pm = pmf.getPersistenceManager();
    	
    	Query q = null;
    	List<Key> keys;
    	try {
    		q = pm.newQuery("select key from " + User.class.getName());
    		keys = (List<Key>)q.execute();
    		Collections.shuffle(keys);
    	} finally {
    		pm.close();
    		q.closeAll();
    	}
    	
    	return keys;
	}
    
    
    @SuppressWarnings("unchecked")
	@Override
    public List<Object> execute(String filter, String declareParameters, String ordering, Long[] queryRange,
            Map<String, Object> parameters, List<FieldAccessorField> touchFields) {
    	PersistenceManager pm = pmf.getPersistenceManager();
    	
        // get results
    	List<Object> users = new ArrayList<Object>();
    	Query query = null;
        try {
        	query = pm.newQuery(User.class);
        	
        	if(filter != null && !filter.isEmpty())
        		query.setFilter(filter);
            
        	if(declareParameters != null && !declareParameters.isEmpty())
        		query.declareParameters(declareParameters);
            
        	if(ordering != null && !ordering.isEmpty())
        		query.setOrdering(ordering);
        		
        	if(queryRange != null && queryRange.length == 2)
        		query.setRange(queryRange[0], queryRange[1]);
        	
        	List<Object> results = null;
        	if(parameters != null && parameters.size() > 0)
        		results = (List<Object>) query.executeWithMap(parameters);
        	else
        		results = (List<Object>) query.execute();
        	
            for (Object result : results) {
                User user = (User) result;
                if(user != null) {
                	FieldAccessor.accessFields(user, null, null, touchFields);
                	cache.put(user.getKey(), user);
                }
                users.add(user);
            }
        } finally {
            query.closeAll();
            pm.close();
        }
        
        return users;
    }

    @Override
    public boolean delete(Object value) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
